package com.app.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.common.Result;
import com.app.model.AdminUserModel;
import com.app.model.ClaimsSettlementModel;
import com.app.model.DisasterReportingModel;
import com.app.server.AdminUserService;
import com.app.server.ClaimsSettlementService;
import com.app.server.DisasterReportingService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.app.vo.FileVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

import static com.app.common.SystemConstant.*;

@RestController
@RequestMapping("/sbbz/claimsSettlement")
public class ClaimsSettlementController {
    @Autowired
    private ClaimsSettlementService claimsSettlementService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private DisasterReportingService disasterReportingService;
    /**
     * 新增和编辑
     *
     * @return {@link Result}
     */
    @PostMapping("/save")
    public Result articleSave(@RequestBody @Valid ClaimsSettlementModel claimsSettlementModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminStringId = TokenUtils.verify(token);
        if (adminStringId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        Integer adminId = Integer.valueOf(adminStringId);
        AdminUserModel adminUser = adminUserService.getById(adminId);
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        if (claimsSettlementModel.getFileList() != null) {
            for (FileVo fileVo : claimsSettlementModel.getFileList()) {
                String fileExtension = "";
                int dotIndex = fileVo.getName().lastIndexOf('.');
                if (dotIndex >= 0) {
                    fileExtension = fileVo.getName().substring(dotIndex + 1);
                }
                if (DEFAULT_type1.contains(fileExtension)) {
                    fileVo.setType("image");
                }
                if (DEFAULT_type3.contains(fileExtension)) {
                    fileVo.setType("pdf");
                }
                if (DEFAULT_type2.contains(fileExtension)) {
                    fileVo.setType("video");
                }
            }
            claimsSettlementModel.setRelatedMaterials(JSON.toJSONString(claimsSettlementModel.getFileList()));
        }
        if (claimsSettlementModel.getId() != null && claimsSettlementModel.getId() > 0) {
            ClaimsSettlementModel settlementModel = claimsSettlementService.getById(claimsSettlementModel.getId());
            if (settlementModel != null) {
                claimsSettlementModel.setAdminId(adminUser.getId());
                claimsSettlementModel.setUpdateTime(CommonHelp.getNowTimestamp());
                DisasterReportingModel model = disasterReportingService.getById(claimsSettlementModel.getReportId());
                model.setClaimsSettlementStatus(claimsSettlementModel.getClaimsSettlementStatus());
                this.claimsSettlementService.updateById(claimsSettlementModel);
                disasterReportingService.updateById(model);
                return Result.success("");
            } else {
                return Result.success("编辑失败");
            }
        } else {
            claimsSettlementModel.setAdminId(adminUser.getId());
            claimsSettlementModel.setCreateTime(CommonHelp.getNowTimestamp());
            this.claimsSettlementService.save(claimsSettlementModel);
            DisasterReportingModel model = disasterReportingService.getById(claimsSettlementModel.getReportId());
            model.setClaimsSettlementStatus(claimsSettlementModel.getClaimsSettlementStatus());
            disasterReportingService.updateById(model);
            return Result.success("");
        }
    }

    /**
     * 详图
     *
     * @param id id
     * @return {@link Result}
     */
    @GetMapping("detail")
    private Result detail(Integer id) {
        QueryWrapper<ClaimsSettlementModel> claimsSettlementModelQueryWrapper = new QueryWrapper<>();
        claimsSettlementModelQueryWrapper.eq("report_id", id);
        ClaimsSettlementModel model = claimsSettlementService.getOne(claimsSettlementModelQueryWrapper);
        if (model != null) {
            if (StrUtil.isNotBlank(model.getRelatedMaterials())) {
                List<FileVo> list = JSON.parseObject(model.getRelatedMaterials(), new TypeReference<List<FileVo>>() {
                });
                model.setFileList(list);
            }

            return Result.success(model);
        } else {
            return null;
        }
    }
}
